package com.bdqn.desiremall.command;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;

import com.bdqn.desiremall.entity.Mall_User;
import com.bdqn.desiremall.serivce.user.UserService;
import com.bdqn.desiremall.serivce.user.UserServiceImpl;
import com.mvc.command.ICommand;

public class UserCommand implements ICommand {
	UserService userService = new UserServiceImpl();
	Mall_User mall_User = null;
	

	public void login(HttpServletRequest request, HttpServletResponse response) {
		
		try {
		
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		String userName = request.getParameter("userName");
		String userPassword = request.getParameter("userPassword");
		mall_User = userService.login(userName, userPassword);
		if (mall_User != null) {
			
			PrintWriter outPrintWriter = null;
			try {
				outPrintWriter = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			outPrintWriter.write("1");
			outPrintWriter.flush();
			outPrintWriter.close();
				
		} else {
			try {
				response.sendRedirect("Login.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void reg(HttpServletRequest request, HttpServletResponse response) {
		String userName = request.getParameter("userName");
		String userPassword = request.getParameter("userPassword");
		String userEmail = request.getParameter("userEmail");
		String userMobileCode = request.getParameter("userMobileCode");
		Integer userGender = Integer
				.valueOf(request.getParameter("userGender"));
		// 获取系统当前时间
		Date userRegistrationTime = new Date();
		mall_User=new Mall_User();
		mall_User.setUserName(userName);
		mall_User.setUserPassword(userPassword);
		mall_User.setUserGender(userGender);
		mall_User.setUserEmail(userEmail);
		mall_User.setUserMobileCode(userMobileCode);
		mall_User.setUserRegistrationTime(userRegistrationTime);
		if (userService.register(mall_User)) {
			try {
				response.sendRedirect("Login.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			try {
				response.sendRedirect("Login.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}
